﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Spreadsheet;
using OPENXMLLib;
using System.IO.Compression;
using DSheet.Core.Data;

namespace DSheet.DocLib
{
    public class DocManager
    {
        public string CreateDoc(Guid id, string path, DocInstance docInst, string data1, string data2, string data3)
        {
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }

            string filePath = string.Format("{0}//{1}.xlsx", path, id);
            if (!IsExist(id, path))
            {
                using (Stream stream = new FileStream(filePath, FileMode.CreateNew))
                {
                    SpreadsheetDocument doc = SpreadsheetDocument.Create(stream, SpreadsheetDocumentType.Workbook);
                    WorkbookPart workbookPart = doc.AddWorkbookPart();
                    workbookPart.Workbook = new DocumentFormat.OpenXml.Spreadsheet.Workbook();

                    Sheets sheets = doc.WorkbookPart.Workbook.AppendChild<Sheets>(new Sheets());

                    for (int index = 1; index < 3; index++)
                    {
                        WorksheetPart worksheetPart = workbookPart.AddNewPart<WorksheetPart>();
                        SheetData sheetData = new SheetData();
                        worksheetPart.Worksheet = new Worksheet(sheetData);


                        if (index == 1)
                        {
                            Sheet sheet = new Sheet()
                            {
                                Id = doc.WorkbookPart.GetIdOfPart(worksheetPart),
                                SheetId = (uint)index,
                                Name = "core"
                            };
                            sheets.Append(sheet);

                            if (worksheetPart.Worksheet.SheetProperties == null)
                            {
                                worksheetPart.Worksheet.SheetProperties = new SheetProperties();
                            }

                            if (worksheetPart.Worksheet.SheetProperties.CodeName == null)
                            {
                                worksheetPart.Worksheet.SheetProperties.CodeName = new StringValue();
                            }
                            worksheetPart.Worksheet.SheetProperties.CodeName = "core";
                            
                            sheetData.GetRow((uint)1, true).GetCell("A", true).XSetSharedStringValue(data1);
                            sheetData.GetRow((uint)1, true).GetCell("B", true).XSetSharedStringValue(data2);
                            sheetData.GetRow((uint)1, true).GetCell("C", true).XSetSharedStringValue(data3);
                        }
                        else
                        {
                            Sheet sheet = new Sheet()
                            {
                                Id = doc.WorkbookPart.GetIdOfPart(worksheetPart),
                                SheetId = (uint)index,
                                Name = docInst.Name
                            };
                            sheets.Append(sheet);

                            if (worksheetPart.Worksheet.SheetProperties == null)
                            {
                                worksheetPart.Worksheet.SheetProperties = new SheetProperties();
                            }

                            if (worksheetPart.Worksheet.SheetProperties.CodeName == null)
                            {
                                worksheetPart.Worksheet.SheetProperties.CodeName = new StringValue();
                            }
                            worksheetPart.Worksheet.SheetProperties.CodeName = docInst.Name;
                        }
                    }

                    workbookPart.Workbook.Save();
                    doc.Close();
                }
            }
            return filePath;
        }

        public bool IsExist(Guid id, string path)
        {
            string filePath = string.Format("{0}//{1}.xlsx", path, id);
            if (File.Exists(filePath))
            {
                return true;
            }
            return false;
        }
    }
}
